

export 'dart:convert';
export 'package:moonplay/common/common_package.dart';
export 'package:moonplay/common/common_size.dart';
export 'package:moonplay/common/common_style.dart';
export 'package:moonplay/common/common_height.dart';
export 'package:moonplay/api/api.dart';

export 'package:moonplay/utils/http_utils.dart';
export 'package:moonplay/utils/image_utils.dart';

export 'package:moonplay/widgets/common_text_widget.dart';


import 'package:bot_toast/bot_toast.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:logger/logger.dart';
import 'package:moonplay/widgets/common_text_widget.dart';    //日志打印

export 'package:flustars/flustars.dart';



var logger = Logger(
  printer: PrettyPrinter(),
);


///设置iconfont
class Constants {
  static const IconFontFamily = 'appIconFont';
}

void CpToast(String text){
  BotToast.showText(text: text, textStyle: TextStyle(fontSize: ScreenUtil().getSp(14), color: Colors.white),align: Alignment(0, 0.7));
}

/**解析视频时长函数 */
dynamic DurationCall(value){
  String _call;
  String _strMin;
  String _strSec;
  int _callMin = value.inMinutes;         //分
  int _callSec = value.inSeconds;         //秒
  _strMin = _callMin < 10 ? '0' + _callMin.toString() : _callMin.toString();      //计算分钟
  if(_callMin > 0) {    //计算秒
    _strSec = (_callSec - _callMin*60) < 10 ? '0' + (_callSec - _callMin*60).toString() : (_callSec - _callMin*60).toString();
  } else {
    _strSec = _callSec.toString();
  }
  _call = _strMin + ':' + _strSec;
  return _call;
}

Widget commonFutureBuilder(context, {@required future, @required callWidget, callTap}){
  return FutureBuilder(
    future: future,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      // print(snapshot.connectionState);
      if (snapshot.connectionState == ConnectionState.waiting) {
        return Center(
          child: Container(
            width: 40,
            height: 40,
            child: SpinKitDualRing(color: Colors.white54,size: 30, lineWidth: 2),
          )
          // Shimmer.fromColors(
          //   baseColor: Colors.red,
          //   highlightColor: Colors.yellow,
          //   child: Text(
          //     '文字光影页面',
          //     textAlign: TextAlign.center,
          //     style: TextStyle(
          //       fontSize: 30.0,
          //       fontWeight:
          //       FontWeight.bold,
          //     ),
          //   ),
          // ),
          // Image.network(
          //   'http://a6611c2f7b2459ce9167.qiniucdn.apicloud-system.com/apicloud/9a996016a287b600392f78f4a782426d.gif',
          //   width: 250,
          //   height: 250,
          //   fit: BoxFit.cover,
          // ),
        );
      } else if (snapshot.connectionState == ConnectionState.done) {
        if(snapshot.hasData) {
            // 正常
          // return Text(snapshot.data["name"]);
          return callWidget;
        } else {
            // 异常
          return InkResponse(
            highlightColor: Colors.transparent,
            radius: 0.0,
            onTap: (){
              print('点击重试');
              callTap();
            },
            child: Center(
              child: Cptext(text: '错误，点击屏幕重试', style: TextStyle(color: Color(0xff888888)),),
            ),
          );
        }
      }
    }
  );
}

class CommonUnderlineTabIndicator extends Decoration {
  const CommonUnderlineTabIndicator({
    this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
    this.insets = EdgeInsets.zero, 
    this.lineColor,
  }) : assert(borderSide != null),
        assert(insets != null);

  final BorderSide borderSide;
  final EdgeInsetsGeometry insets;
  final Color lineColor;

  @override
  Decoration lerpFrom(Decoration a, double t) {
    if (a is CommonUnderlineTabIndicator) {
      return CommonUnderlineTabIndicator(
        borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
        insets: EdgeInsetsGeometry.lerp(a.insets, insets, t),
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
  Decoration lerpTo(Decoration b, double t) {
    if (b is CommonUnderlineTabIndicator) {
      return CommonUnderlineTabIndicator(
        borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
        insets: EdgeInsetsGeometry.lerp(insets, b.insets, t),
        lineColor: Color.lerp(lineColor, b.lineColor, t),
      );
    }
    return super.lerpTo(b, t);
  }

  @override
  _UnderlinePainter createBoxPainter([ VoidCallback onChanged ]) {
    return _UnderlinePainter(this, onChanged);
  }
}

class _UnderlinePainter extends BoxPainter {
  _UnderlinePainter(this.decoration, VoidCallback onChanged)
      : assert(decoration != null),
        super(onChanged);

  final CommonUnderlineTabIndicator decoration;

  BorderSide get borderSide => decoration.borderSide;
  EdgeInsetsGeometry get insets => decoration.insets;
  Color get lineColor => decoration.lineColor;

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    assert(rect != null);
    assert(textDirection != null);
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);

    //线条宽度
    double wantWidth = 14;
    //取它的中间坐标
    double cw = (indicator.left + indicator.right) / 2;
    return Rect.fromLTWH(cw - wantWidth / 2,
        indicator.bottom - borderSide.width, wantWidth, borderSide.width);
  }

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    ///渐变色颜色
    final Gradient gradient = new LinearGradient(
      colors: [
        lineColor,
        lineColor
      ],
    );
    assert(configuration != null);
    assert(configuration.size != null);
    final Rect rect = offset & configuration.size;
    final TextDirection textDirection = configuration.textDirection;
    final Rect indicator = _indicatorRectFor(rect, textDirection).deflate(borderSide.width / 2.0);
    final Paint paint = borderSide.toPaint()..strokeCap = StrokeCap.square;
    paint.shader = gradient.createShader(indicator);
    canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
  }
}

seekTime(){
  int _time = 0;
  int _minute;
  int _second;
  String _startTime = SpUtil.getString('startTime');
  if(_startTime != null) {
    if(_startTime.length == 3) {
      _minute = int.parse(_startTime.substring(0, 1));
      _second = int.parse(_startTime.substring(1));
    } else {
      _minute = 0;
      _second = int.parse(_startTime);
    }
    _time = (_minute*60 + _second)*1000;
  }
  return _time;
}